package easytasks.ui;

import java.io.Serializable;
import java.util.List;

import easytasks.data.DataManager;
import easytasks.data.DataSetup;
import easytasks.data.Task;
import easytasks.data.TaskCategory;
import easytasks.logic.DateFormatter;
import easytasks.logic.DateFormatter.DateAsStringType;
import easytasks.main.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class TaskActivity extends Activity 
{
	private Task task;
	
	private AppMenu appMenu;
	private TextView textTitle = null;
	private TextView textDate = null;
	private TextView textCategories = null;
	private TextView textDescription = null;
	private CheckBox checkDone = null;
	private ImageButton butTaskEdit;
	private ImageButton butDelete;
	
	private DataSetup dataInit;
	private DataManager dataManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task);
        
        Serializable taskReceived = getIntent().getSerializableExtra("task");
        if (taskReceived == null) throw new IllegalArgumentException("Input task not defined.");
        if (!(taskReceived instanceof Task)) throw new IllegalArgumentException("Input task not valid.");
        task = (Task)taskReceived;
        
        appMenu = new AppMenu(this);
        textTitle = (TextView)findViewById(R.id.textTaskTitle);
        textDate = (TextView)findViewById(R.id.textTaskDate);
        textCategories = (TextView)findViewById(R.id.textTaskCategories);
        textDescription = (TextView)findViewById(R.id.textTaskDescription);
        checkDone = (CheckBox)findViewById(R.id.checkTaskDone);
        butTaskEdit = (ImageButton)findViewById(R.id.butTaskEdit);
        butDelete = (ImageButton)findViewById(R.id.butTaskDelete);
        
        checkDone.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
			{
				dataManager.markTask(task, isChecked);
				appMenu.refresh();
			}
		});
        
        butTaskEdit.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) 
			{
				Intent intent = new Intent(TaskActivity.this, EditTaskActivity.class);
				intent.putExtra("task", task);
				startActivity(intent);
			}
		});
        
        butDelete.setOnClickListener(new OnClickListener() {
			public void onClick(View v) 
			{
				AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(TaskActivity.this);
				dialogBuilder.setTitle(getResources().getString(R.string.task_delete_confirmation_title));
				dialogBuilder.setMessage(getResources().getString(R.string.task_delete_confirmation_message));
				
				dialogBuilder.setPositiveButton(R.string.task_delete_confirmation_yes, new DialogInterface.OnClickListener() {					
					public void onClick(DialogInterface dialog, int which)
					{
						dataManager.removeTask(task);
						finish();
					}
				});

				dialogBuilder.setNegativeButton(R.string.task_delete_confirmation_no, null);
				dialogBuilder.create().show();
			}
		});
        
        dataInit = new DataSetup(this);
        dataManager = new DataManager(dataInit, PreferenceManager.getDefaultSharedPreferences(this));
    }
    
    @Override
    public void onResume()
    {
    	super.onResume();
    	refresh();
    }

	private void refresh()
	{
		// We must refresh current task (it could be modified)
		task = dataManager.getTaskById(task.getId());
		
		textTitle.setText(task.getTitle());
		textDate.setText((new DateFormatter(this)).getTimeAsString(task.getTime(), DateAsStringType.WEEKDAYDAY_AND_LONGMONTHANDHOUR, task.getIgnoreHour()));
		
		String categoriesText = "";
		List<TaskCategory> categories = task.getCategories();
		int categoryCount = categories.size();
		
		for (int i = 0; i < categoryCount; i++)
		{
			categoriesText += categories.get(i).getName();
			if (i < (categoryCount - 1)) categoriesText += ", ";
		}
		
		textCategories.setText(categoriesText);
		textDescription.setText(task.getDescription());
		checkDone.setChecked(task.isDone());
		appMenu.refresh();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu)	
    {
    	super.onCreateOptionsMenu(menu);
    	return appMenu.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	return appMenu.onOptionsItemSelected(item);
    }
}
